# Copyright 2022 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_python//python:proto.bzl", "py_proto_library")
load("//pw_build:pigweed.bzl", "pw_cc_binary")

pw_cc_binary(
    name = "server",
    srcs = ["server.cc"],
    deps = [
        ":config_cc_proto",
        "//pw_assert",
        "//pw_chrono:system_clock",
        "//pw_log",
        "//pw_rpc/system_server",
        "//pw_stream",
        "//pw_stream:std_file_stream",
        "//pw_thread:thread",
        "//pw_transfer",
        "@com_google_protobuf//:protobuf",
    ],
)

py_binary(
    name = "proxy",
    srcs = ["proxy.py"],
    deps = [
        ":config_pb2",
        "//pw_hdlc/py:pw_hdlc",
        "//pw_transfer:transfer_proto_pb2",
        "//pw_transfer/py:pw_transfer",
        "@com_google_protobuf//:protobuf_python",
    ],
)

py_test(
    name = "proxy_test",
    srcs = [
        "proxy.py",
        "proxy_test.py",
    ],
    imports = ["."],
    main = "proxy_test.py",
    deps = [
        ":config_pb2",
        "//pw_hdlc/py:pw_hdlc",
        "//pw_rpc:internal_packet_proto_pb2",
        "//pw_transfer:transfer_proto_pb2",
        "//pw_transfer/py:pw_transfer",
    ],
)

proto_library(
    name = "config_proto",
    srcs = ["config.proto"],
    deps = ["//pw_protobuf:status_proto"],
)

cc_proto_library(
    name = "config_cc_proto",
    deps = [":config_proto"],
)

py_proto_library(
    name = "config_pb2",
    deps = [":config_proto"],
)

java_proto_library(
    name = "config_java_proto",
    deps = [":config_proto"],
)

pw_cc_binary(
    name = "cpp_client",
    srcs = ["client.cc"],
    deps = [
        ":config_cc_proto",
        "//pw_log",
        "//pw_rpc:integration_testing",
        "//pw_status",
        "//pw_stream:std_file_stream",
        "//pw_sync:binary_semaphore",
        "//pw_thread:thread",
        "//pw_transfer",
        "//pw_transfer:client",
        "@com_google_protobuf//:protobuf",
    ],
)

py_library(
    name = "integration_test_fixture",
    srcs = [
        "test_fixture.py",
    ],
    data = [
        ":cpp_client",
        ":java_client",
        ":proxy",
        ":python_client",
        ":server",
    ],
    imports = ["."],
    deps = [
        ":config_pb2",
        "//pw_protobuf:status_proto_pb2",
        "@com_google_protobuf//:protobuf_python",
        "@rules_python//python/runfiles",
    ],
)

# Uses ports 3310 and 3311.
py_test(
    name = "cross_language_large_write_test",
    # Actually 1 hour, see
    # https://docs.bazel.build/versions/main/test-encyclopedia.html#role-of-the-test-runner
    timeout = "eternal",
    srcs = [
        "cross_language_large_write_test.py",
    ],
    tags = [
        # This test is not run in CQ because it's too slow.
        "manual",
        "integration",
    ],
    deps = [
        ":integration_test_fixture",
    ],
)

# Uses ports 3306 and 3307.
py_test(
    name = "cross_language_large_read_test",
    # Actually 1 hour, see
    # https://docs.bazel.build/versions/main/test-encyclopedia.html#role-of-the-test-runner
    timeout = "eternal",
    srcs = [
        "cross_language_large_read_test.py",
    ],
    tags = [
        # This test is not run in CQ because it's too slow.
        "manual",
        "integration",
    ],
    deps = [
        ":integration_test_fixture",
    ],
)

# Uses ports 3304 and 3305.
py_test(
    name = "cross_language_medium_read_test",
    timeout = "long",
    srcs = [
        "cross_language_medium_read_test.py",
    ],
    tags = [
        "integration",
    ],
    deps = [
        ":config_pb2",
        ":integration_test_fixture",
        "@com_google_protobuf//:protobuf_python",
        "@python_packages_parameterized//:pkg",
    ],
)

# Uses ports 3316 and 3317.
py_test(
    name = "cross_language_medium_write_test",
    timeout = "long",
    srcs = [
        "cross_language_medium_write_test.py",
    ],
    tags = [
        "integration",
    ],
    deps = [
        ":config_pb2",
        ":integration_test_fixture",
        "@com_google_protobuf//:protobuf_python",
        "@python_packages_parameterized//:pkg",
    ],
)

# Uses ports 3302 and 3303.
py_test(
    name = "cross_language_small_test",
    timeout = "moderate",
    srcs = [
        "cross_language_small_test.py",
    ],
    tags = [
        "integration",
    ],
    deps = [
        ":config_pb2",
        ":integration_test_fixture",
        "@python_packages_parameterized//:pkg",
    ],
)

# Uses ports 3308 and 3309.
py_test(
    name = "multi_transfer_test",
    timeout = "moderate",
    srcs = [
        "multi_transfer_test.py",
    ],
    tags = [
        "integration",
    ],
    deps = [
        ":config_pb2",
        ":integration_test_fixture",
        "@python_packages_parameterized//:pkg",
    ],
)

# Uses ports 3312 and 3313.
py_test(
    name = "expected_errors_test",
    timeout = "moderate",
    srcs = ["expected_errors_test.py"],
    tags = [
        "integration",
    ],
    deps = [
        ":config_pb2",
        ":integration_test_fixture",
        "//pw_protobuf:status_proto_pb2",
        "@com_google_protobuf//:protobuf_python",
        "@python_packages_parameterized//:pkg",
    ],
)

# Uses ports 3314 and 3315.
py_test(
    name = "legacy_binaries_test",
    timeout = "moderate",
    srcs = ["legacy_binaries_test.py"],
    data = [
        "@pw_transfer_test_binaries//:all",
    ],
    tags = [
        "integration",
    ],
    # Legacy binaries were only built for linux-x86_64.
    target_compatible_with = ["@platforms//os:linux"],
    deps = [
        ":config_pb2",
        ":integration_test_fixture",
        "//pw_protobuf:status_proto_pb2",
        "@python_packages_parameterized//:pkg",
        "@rules_python//python/runfiles",
    ],
)

java_binary(
    name = "java_client",
    srcs = ["JavaClient.java"],
    main_class = "JavaClient",
    deps = [
        ":config_java_proto",
        "//pw_hdlc/java/main/dev/pigweed/pw_hdlc",
        "//pw_log/java/main/dev/pigweed/pw_log",
        "//pw_rpc/java/main/dev/pigweed/pw_rpc:client",
        "//pw_transfer/java/main/dev/pigweed/pw_transfer:client",
        "@com_google_protobuf//:protobuf_java",
        "@maven//:com_google_flogger_flogger_system_backend",
        "@maven//:com_google_guava_guava",
    ],
)

py_binary(
    name = "python_client",
    srcs = ["python_client.py"],
    deps = [
        ":config_pb2",
        "//pw_hdlc/py:pw_hdlc",
        "//pw_rpc/py:pw_rpc",
        "//pw_transfer:transfer_proto_pb2",
        "//pw_transfer/py:pw_transfer",
        "@com_google_protobuf//:protobuf_python",
        "@python_packages_pyserial//:pkg",
    ],
)
